/*
 *	@file	fld_cb.c
 *	@brief	カスタムボールアプリ　フィールドイベント常駐処理系
 *	@author	Miyuki Iwasawa
 *	@date	08.04.13
 */

#include "common.h"

#include "field/field_common.h"
#include "field/field_event.h"
#include "field/fieldsys.h"
#include "field/ev_mapchange.h"

#include "seal/seal.h"
#include "application/pokelist.h"
#include "include/field/tvtopic_extern.h"
#include "include/savedata/tv_work.h"

#include "application/cb_app.h"
#include "fld_cb.h"
#include "system/pm_overlay.h"

typedef struct {
	int seq;
} CB_EDIT_WORK;

typedef struct {
	CB_EDIT_WORK*	edit;
	CB_PROC_WORK*	p_wk;
	PLIST_DATA*		pld;
	SAVEDATA*		save;

	int seq;

} CB_SUB_EDIT_WORK;

enum {
	CBPS_INIT = 0,
	CBPS_MAIN,
	CBPS_MAIN_END_CHECK,
	CBPS_LIST,
	CBPS_LIST_END_CHECK,
	CBPS_EXIT,
	CBPS_FREE,
};

///////////////////////////////////////////////////////////////////////////
//プロトタイプ
///////////////////////////////////////////////////////////////////////////
static BOOL GMEVENT_Sub_CBEdit(GMEVENT_CONTROL * event);

///////////////////////////////////////////////////////////////////////////
//メインソースエリア
///////////////////////////////////////////////////////////////////////////

/*
 *	@brief	カスタムボール編集プロセス呼び出し
 */
void EventCmd_CustomBallEditProc(GMEVENT_CONTROL * event, SAVEDATA* save)
{
	FIELDSYS_WORK*	fsys = FieldEvent_GetFieldSysWork(event);
	CB_SUB_EDIT_WORK* sub_edit = sys_AllocMemoryLo(HEAPID_WORLD, sizeof(CB_SUB_EDIT_WORK));
	memset(sub_edit, 0, sizeof(CB_SUB_EDIT_WORK));

	sub_edit->save	= save;

	///< プロックワーク
	sub_edit->p_wk	= sys_AllocMemory(HEAPID_WORLD, sizeof(CB_PROC_WORK));
	memset(sub_edit->p_wk, 0, sizeof(CB_PROC_WORK));
	sub_edit->p_wk->cfg = SaveData_GetConfig(save);
	sub_edit->p_wk->save = save;
	sub_edit->p_wk->kt_status = &fsys->KeyTouchStatus;

	///< ポケモンリスト
	sub_edit->pld	= sys_AllocMemory( HEAPID_WORLD, sizeof(PLIST_DATA) );
	memset(sub_edit->pld, 0, sizeof(PLIST_DATA));

	FieldEvent_Call(event, GMEVENT_Sub_CBEdit, sub_edit);
}

static BOOL GMEVENT_Sub_CBEdit(GMEVENT_CONTROL * event)
{
	CB_SUB_EDIT_WORK*	sub_edit = FieldEvent_GetSpecialWork(event);
	CB_PROC_WORK*		p_wk	 = sub_edit->p_wk;
	FIELDSYS_WORK*		fsys	 = FieldEvent_GetFieldSysWork(event);

	switch(sub_edit->seq){
	case CBPS_INIT:
		///< 初期化
		EventCmd_FinishFieldMap(event);
		p_wk->save_data = CB_SaveData_AllDataGet(sub_edit->save);
//		CBProc_SelectPosSet(p_wk, 0);
		p_wk->proc_next_mode = 0;
		p_wk->kt_status = &fsys->KeyTouchStatus;

		{
			int i;
			int poke_cnt;
			p_wk->pparty = SaveData_GetTemotiPokemon(sub_edit->save);
			poke_cnt = PokeParty_GetPokeCount(p_wk->pparty);
			p_wk->poke_cnt = poke_cnt;
			for (i = 0; i < poke_cnt; i++){
				p_wk->poke_para[i] = PokeParty_GetMemberPointer(p_wk->pparty, i);
			}
			for ( ; i < TEMOTI_POKEMAX; i++){
				p_wk->poke_para[i] = NULL;
			}
		}
		sub_edit->seq = CBPS_MAIN;
		break;

	case CBPS_MAIN:
		{
			FS_EXTERN_OVERLAY(custom_ball);
			static const PROC_DATA CustomBallProcData = {		///< プロセス定義データ
				CB_ProcInit,
				CB_ProcMain,
				CB_ProcEnd,

				FS_OVERLAY_ID(custom_ball),
			};

			///< カプセル画面
			EventCmd_CallSubProc(event, &CustomBallProcData, p_wk);
		}
		sub_edit->seq = CBPS_MAIN_END_CHECK;
		break;

	case CBPS_MAIN_END_CHECK:
		///< リストへ行くかの分岐
		{
			u8 mode;

			mode = sub_edit->p_wk->proc_next_mode;//CBProc_NextModeGet(sub_edit->p_wk);

			switch(mode){
			default:
				GF_ASSERT(0);
			case CB_PROC_NEXT_MODE_END:
				sub_edit->seq = CBPS_EXIT;		///< 終了
				break;

			case CB_PROC_NEXT_MODE_LIST:
				sub_edit->seq = CBPS_LIST;		///< リスト
				break;
			}
		}
		break;

	case CBPS_LIST:
		///< リスト処理
		{
			PLIST_DATA* pld	= sub_edit->pld;
			pld->pp			= p_wk->pparty;
			pld->myitem		= SaveData_GetMyItem(sub_edit->save);
			pld->mailblock	= SaveData_GetMailBlock(sub_edit->save);
			pld->ret_sel	= 0;
			pld->type		= PL_TYPE_SINGLE;
			pld->mode		= PL_MODE_KAPSEL_SET;
			pld->cfg		= p_wk->cfg;
			pld->fsys       = fsys;
			pld->kt_status  = &fsys->KeyTouchStatus;

			EventCmd_CallSubProc(event, &PokeListProcData, pld);
			sub_edit->seq = CBPS_LIST_END_CHECK;
		}
		break;

	case CBPS_LIST_END_CHECK:
		///< カプセルへ戻る準備
		{
			PLIST_DATA* pld	= sub_edit->pld;
			POKEMON_PARAM* pp;
			CB_CORE* core;
			CB_SEAL* seal;
			TV_WORK* tvwk;
			int tv_id;
			int cb_id;

			cb_id = sub_edit->p_wk->select_pos+1;
			if (pld->ret_sel != PL_SEL_POS_EXIT){
				IWASAWA_PRINT("CBallSet poe = %d, ball = %d\n",pld->ret_sel,cb_id);

				pp = sub_edit->p_wk->poke_para[pld->ret_sel];

				PokeParaPut(pp, ID_PARA_cb_id, (u8*)&cb_id);
				PokeParaPut(pp,
							ID_PARA_cb_core,
							CB_SaveData_CoreDataGet(p_wk->save_data, cb_id - 1));

				///< TV用データ ０番目は必ずシールが貼られているので０番目で
				core  = CB_SaveData_CoreDataGet(p_wk->save_data, cb_id - 1);
				seal  = CB_SaveData_SealDataGet(core, 0);
				tv_id = CB_SaveData_SealIDGet(seal);
				tv_id = Seal_NameGet(tv_id);					///< seal id から 名前ＩＤへ変換
				tvwk  = SaveData_GetTvWork(fsys->savedata);
#if 0	//TV_OFF
				TVTOPIC_Entry_Watch_CustomBall(tvwk, pp, tv_id);
#endif
			}
		}
		sub_edit->seq = CBPS_MAIN;
		break;

	case CBPS_EXIT:
		///< 終了処理
		EventCmd_StartFieldMap(event);
		sub_edit->seq = CBPS_FREE;
		break;

	case CBPS_FREE:
		///< 解放処理
		sys_FreeMemoryEz(sub_edit->pld);
		sys_FreeMemoryEz(sub_edit->p_wk);
		sys_FreeMemoryEz(sub_edit);
		return TRUE;
	}
	return FALSE;
}

///< デバッグから呼ばれる
#ifdef PM_DEBUG
static BOOL GMEVENT_CBEdit(GMEVENT_CONTROL * event)
{
	FIELDSYS_WORK*	fsys = FieldEvent_GetFieldSysWork(event);
	CB_EDIT_WORK*	edit = FieldEvent_GetSpecialWork(event);

	switch(edit->seq){
	case 0:
		EventCmd_FieldFadeOut(event);
		edit->seq++;
		break;
	case 1:
		EventCmd_CustomBallEditProc(event, fsys->savedata);
		edit->seq++;
		break;
	case 2:
		EventCmd_FieldFadeIn(event);
		edit->seq++;
		break;
	case 3:
		sys_FreeMemoryEz(edit);
		return TRUE;
	}

	return FALSE;
}

/**
 *	@brief	カスタムボール編集　デバッグプロセス呼び出し
 */
void CustomBallEdit_Start(FIELDSYS_WORK *fsys)
{
	CB_EDIT_WORK* edit;

	edit = sys_AllocMemoryLo(HEAPID_WORLD, sizeof(CB_EDIT_WORK));

	edit->seq = 0;

	FieldEvent_Set(fsys, GMEVENT_CBEdit, edit);
}

#endif	//PM_DEBUG
